<template>
  <div class="map">
    <el-select v-model="formData.province" value-key="id" filterable remote reserve-keyword remote-show-suffix>
      <el-option v-for="item in regions" :key="item.id" :label="item.name" :value="item" />
    </el-select>
    <el-select v-model="formData.city" value-key="id" filterable remote reserve-keyword remote-show-suffix>
      <el-option v-for="item in formData.province.children" :key="item.adcode" :label="item.name" :value="item" />
    </el-select>
    <el-select v-model="formData.district" value-key="id" filterable remote reserve-keyword remote-show-suffix>
      <el-option v-for="item in formData.city.children" :key="item.id" :label="item.name" :value="item" />
    </el-select>
    <el-input class="map-input" v-model="formData.address">
      <template #append>
        <el-button @click="dialogVisible = true">
          <el-icon><Location /></el-icon>
        </el-button>
      </template>
    </el-input>
  </div>

  <el-dialog v-model="dialogVisible" title="地图控件" center :close-on-click-modal="false" :destroy-on-close="true">
    <Amap @change="onChange" v-model="formData" />
  </el-dialog>
</template>
<script lang="ts" setup name="Amap">
import { ref, reactive, onBeforeMount, watch } from "vue";
import Amap from "./Amap.vue";
import region from "./region.json";

const props = defineProps({
  districtId: {
    type: [String, Number],
    default: 0
  },
  address: {
    type: String,
    default: ""
  },
  lat: {
    type: [String, Number],
    default: 0
  },
  lng: {
    type: [String, Number],
    default: 0
  },
  modelValue: {
    type: [Object, String]
  }
});

const dialogVisible = ref(false);

const formData = reactive({
  province: "",
  city: "",
  district: "",
  address: "",
  lat: "",
  lng: ""
});

watch(props, newValue => {
  formData.address = newValue.address;
  formData.address = newValue.address;
  formData.lat = newValue.lat;
  formData.lng = newValue.lng;
  init();
});

const init = () => {
  if (props.districtId) {
    regions.value.forEach(province => {
      province.children.forEach(city => {
        if (props.districtId) {
          city.children.forEach(district => {
            if (district.id == props.districtId) {
              formData.province = province;
              formData.city = city;
              formData.district = district;
              return;
            }
          });
        }
      });
    });
  }

  formData.address = props.address;
};

const emits = defineEmits([
  "change",
  "update:provinceId",
  "update:cityId",
  "update:districtId",
  "update:address",
  "update:lat",
  "update:lng"
]);

watch(formData, newValue => {
  if (!newValue.province) {
    formData.city = "";
    formData.district = "";
  } else if (!newValue.city || newValue.city.parent_id != newValue.province.id) {
    formData.city = "";
    formData.district = "";
  } else if (newValue.district.parent_id != newValue.city.id) {
    formData.district = "";
  }

  const form = {
    province_id: newValue.province?.id,
    province_name: newValue.province?.name,
    city_id: newValue.city?.id,
    city_name: newValue.city?.name,
    district_id: newValue.district?.id,
    district_name: newValue.district?.name,
    lat: newValue.lat,
    lng: newValue.lng,
    address: newValue.address
  };

  emits("change", form, newValue.district?.id, newValue.address, newValue.lng, newValue.lat);
  emits("update:provinceId", newValue.province?.id);
  emits("update:cityId", newValue.city?.id);
  emits("update:districtId", newValue.district?.id);
  emits("update:lat", newValue.lat);
  emits("update:lng", newValue.lng);
  emits("update:address", newValue.address);
});

const regions = ref(region[0].children);
onBeforeMount(init);
const onChange = form => {
  formData.address = form.address;
  formData.province = form.province;
  formData.city = form.city;
  formData.district = form.district;
  formData.lat = form.lat;
  formData.lng = form.lng;
};
</script>
<style scoped>
.map {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-gap: 10px;
  width: 100%;
  padding: 0 2px;
  margin-bottom: 5px;
}
.map-input {
  grid-column: span 2;
}
</style>
